// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
const { show, msg } = require("../game/alert");
const EventServices = require("../util/EventServices");
const { shopData } = require("./gameData");

cc.Class({
    extends: cc.Component,

    properties: {
        price: cc.Node,
        level: cc.Label,
        levelCost: cc.Label
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.node.on('on_scroll_view', (opacity) => {
            this.node.opacity = opacity;
        })
    },
    unlock(target, i) {
        let food = shop.menuList.find(food=>{
            return food.id == target.target.parent.id;
        });
        if(food.unlockMoney <= shop.money && food.unlockFamous <= shop.famous){
            food.state = 1;
            show('未来餐饮',"恭喜解锁新配方");
            shop.foodList.push(food);
            let foodIndex = shop.foodList.findIndex(food => food.id == target.target.parent.id);
            shop.famous -= food.unlockFamous;
            shop.money -= food.unlockMoney;
            EventServices.publish('unlockFood', food, foodIndex);
            this.node.destroy();
        }else {
            show('未来餐饮','条件不够呢，老板')
        }

    },
    upPrice(target, count){
        let self = this;
    
        let food = shop.menuList.find(food=>{
            return food.id == target.target.parent.id;
        });
        if(food.level == 6) {
            show('店铺升级','已经是最新配方了噢',);
            return;
        }
        let cost = food.level * shop.level * 200;
        if(shop.money < cost){
            msg("没钱拿头升级e");
            return ;
        }
        food.price += parseInt(food.price * 0.3);
        shop.money -= cost;
        food.level += 1;
        self.price.getComponent(cc.Label).string = parseInt(food.price) + '元';
        food.levelCost = food.level * shop.level * 200;
        this.level.string = food.level + "级";
        this.levelCost.string =  food.levelCost + '元';
        show('店铺升级','恭喜升级了新配方,价格提升20%',);

    },
    ads(target,count){
        let famous = Math.ceil(Math.random()*5);
        shop.famous += famous;
        show('广告提醒','花了100元发传单，知名度提升'+famous);
    }
    // update (dt) {},
});
